package com.ForeHome.app.Controller;

import com.ForeHome.app.Service.CategoryService;
import com.ForeHome.app.Service.ProductImageService;
import com.ForeHome.app.Service.ProductService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tamll.Type.CommoditySort;
import com.tamll.Type.DataType;
import com.tamll.Type.ProductSort;
import com.tamll.bean.Category;
import com.tamll.bean.Product;
import com.tamll.bean.ProductImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class MainGoodsController {


    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductImageService productImageService;


    @GetMapping("goodsCollection")
    public DataType goodsCollection(){
        DataType dataType=new DataType();

//        查询所有分类信息
        Page<Category> page = categoryService.listAll();
        List<CommoditySort> list1=new ArrayList<>();
        for (Category category: page.getRecords()) {
            CommoditySort commoditySort=new CommoditySort();
            commoditySort.setCategory_id(category.getCategoryId());
            commoditySort.setCategory_name(category.getCategoryName());
            List<ProductSort> list=new ArrayList<>();
            //根据分类id 查询到的商品
            List<Product> products = productService.title_category_id(category.getCategoryId());
            for (Product product:products) {
                ProductSort productSort=new ProductSort();
                productSort.setProduct_id(product.getProductId());
                productSort.setProduct_name(product.getProductName());
//                根据商品id查询到的图片集合
                List<ProductImage> productImages = productImageService.productimageProductIdSelect(product.getProductId());
                productSort.setProduct_sale_price(productImages);
                list.add(productSort);
            }
            commoditySort.setList(list);
            list1.add(commoditySort);
        }

        dataType.setCommoditySorts(list1);

        return dataType;
    }



}
